package com.appcheck.recording.listener.impl;

import com.appcheck.recording.action.impl.OrientationChangeAction;

import android.content.Context;
import android.util.Log;
import android.view.OrientationEventListener;

public class OrientationDetector extends OrientationEventListener {

	private int oldOrientation;
	
	public final int getOldOrientation() {
		return oldOrientation;
	}

	public final void setOldOrientation(int oldOrientation) {
		this.oldOrientation = oldOrientation;
	}
	
	public final void updateOrientation(int orientation){
		if(orientation != oldOrientation){
			oldOrientation = orientation;
			OrientationChangeAction action = new OrientationChangeAction();
			action.setOrientation(oldOrientation);
		}
			
	}

	public OrientationDetector(Context context) {
		super(context);
	}

	@Override
	public void onOrientationChanged(int orientation) {
    	if(orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
    	    return;  //手机平放时，检测不到有效的角度
    	}
    	//只检测是否有四个角度的改变
    	if( orientation > 350 || orientation< 10 ) { //0度
    	    updateOrientation(0);
    	}   
    	else if( orientation > 80 &&orientation < 100 ) { //90度
    		updateOrientation(90);
    	}
    	else if( orientation > 170 &&orientation < 190 ) { //180度
    		updateOrientation(180);
    	}
    	else if( orientation > 260 &&orientation < 280  ) { //270度
    		updateOrientation(270);
    	}
    	else {
    	    return;
    	}
        Log.i("MyOrientationDetector ","onOrientationChanged:"+orientation);
	}

}
